package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;

    @Autowired
    private MongoTemplate mongoTemplate;//可以操作数据库

    @Autowired
    private IDestinationService destinationService;//目的地服务类
    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }


    @Override
    public void updateInfo(StrategyCatalog strategyCatalog) {

    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog get(String id) {
        Optional byId = strategyCatalogRepository.findById(id);
//强转
        return (StrategyCatalog) byId.orElse(null);//如果是空是就返回空值,否则返回对象
    }

    @Override
    public List<StrategyCatalog> listAll() {
        List<StrategyCatalog> strategyCatalogs = strategyCatalogRepository.findAll();
        return strategyCatalogs;
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }


    /**
     * 抽取的新增/修改方法
     * @param strategyCatalog
     */
    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
//        1.查询
        //根据关联的字段destId获取获取出对应的目的地对象destination
        String destId = strategyCatalog.getDestId();
        Destination destination = destinationService.get(destId);
        //2.替换
        //获取destination对象的名字设置在strategyCatalog对象的冗余字段destName
        String name = destination.getName();
        strategyCatalog.setDestName(name);
        //再插入数据库或者更新用户改的对象
        if(!StringUtils.hasLength(strategyCatalog.getId())){
            this.save(strategyCatalog);
        }else{
            //3.更新
            this.update(strategyCatalog);
        }


    }
//        //1.查询
//        Destination dest = destinationService.get(strategyCatalog.getDestId());
//        //2.替换
//        String destName = dest.getName();
//        strategyCatalog.setDestName(destName);
//        if (!StringUtils.hasLength(strategyCatalog.getId())) {
//            //新增操作
//            this.save(strategyCatalog);
//        } else {
//            //有值才执行修改操作
////            //修改操作
//
//            //3.更新


//        this.update(strategyCatalog);
//        }
//    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        //查询对象
        Query query = new Query();
        //查询出总数条数
        long rows = mongoTemplate.count(query, StrategyCatalog.class);
        if (rows == 0) {
            return  Page.empty();//如果总数条数是0,就返回空集合
        }
        //分页对象(设置当前页从0开始算,还有页面显示条数,按sequence这个列正序排列 )  
        //  Sort.Direction.ASC,表示按正序排列表格
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        //给查询对象
        query.with(pageable);
        List<StrategyCatalog> list = mongoTemplate.find(query, StrategyCatalog.class);

        return  new PageImpl<>(list,pageable,rows);//相当于pageResult对象

    }

}
